//
//  MyInfoViewController.m
//  StormWatch
//
//  Created by 王岩 on 2024/9/16.
//

#import "MyInfoViewController.h"

static NSString * _userName = @"_userName";
static NSString * _userSex = @"_userSex";
static NSString * _userAge = @"_userAge";
static NSString * _userHeight = @"_userHeight";
static NSString * _userWeight = @"_userWeight";

@interface MyInfoViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *avatar;
@property (weak, nonatomic) IBOutlet UILabel *nameLab;
@property (weak, nonatomic) IBOutlet UILabel *sexLab;
@property (weak, nonatomic) IBOutlet UILabel *sexValue;
@property (weak, nonatomic) IBOutlet UILabel *ageLab;
@property (weak, nonatomic) IBOutlet UILabel *ageValue;
@property (weak, nonatomic) IBOutlet UILabel *heightLab;
@property (weak, nonatomic) IBOutlet UILabel *heightValue;
@property (weak, nonatomic) IBOutlet UILabel *weightLab;
@property (weak, nonatomic) IBOutlet UILabel *weightValue;

@property (weak, nonatomic) IBOutlet UIView *sexView;
@property (weak, nonatomic) IBOutlet UIView *ageView;
@property (weak, nonatomic) IBOutlet UIView *heightView;
@property (weak, nonatomic) IBOutlet UIView *weightView;
/* <#note#> */
@property (nonatomic, strong) UIImagePickerController * imagePickController;
@end

@implementation MyInfoViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = SWString(@"User settings");
    self.avatar.layer.masksToBounds = YES;
    
    [self.avatar addGestureRecognizer:[[UITapGestureRecognizer  alloc] initWithTarget:self action:@selector(clickAvatar)]];
    [self.nameLab addGestureRecognizer:[[UITapGestureRecognizer  alloc] initWithTarget:self action:@selector(clickNameLab)]];
    
    self.sexView.layer.borderColor = S_HexColor(@"0xeeeeee").CGColor;
    self.ageView.layer.borderColor = S_HexColor(@"0xeeeeee").CGColor;
    self.heightView.layer.borderColor =  S_HexColor(@"0xeeeeee").CGColor;
    self.weightView.layer.borderColor =  S_HexColor(@"0xeeeeee").CGColor;
    
    [self.sexView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickSexView)]];
    [self.ageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickAgeView)]];
    [self.heightView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickHeightView)]];
    [self.weightView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickWeightView)]];
    
    self.sexLab.text = SWString(@"Gender");
    self.ageLab.text = SWString(@"Age");
    self.heightLab.text = SWString(@"Height");
    self.weightLab.text = SWString(@"Weight");
    
    NSString * name = [[NSUserDefaults standardUserDefaults] valueForKey:_userName];
    if (name.length) {
        self.nameLab.text = name;
    }else {
        self.nameLab.text = @"Storm";
    }
    
    NSString * sex = [[NSUserDefaults standardUserDefaults] valueForKey:_userSex];
    if (sex.length) {
        self.sexValue.text = [sex isEqualToString:@"Male"]?SWString(@"Male"):SWString(@"Female");
    }else {
        self.sexValue.text = SWString(@"Female");
    }
    NSString * age = [[NSUserDefaults standardUserDefaults] valueForKey:_userAge];
    if (age.length) {
        self.ageValue.text = age;
    }else {
        self.ageValue.text = @"24";
    }
    
    NSString * height = [[NSUserDefaults standardUserDefaults] valueForKey:_userHeight];
    if (height.length) {
        self.heightValue.text = height;
    }else {
        self.heightValue.text = @"170";
    }
    
    NSString * weight = [[NSUserDefaults standardUserDefaults] valueForKey:_userWeight];
    if (weight.length) {
        self.weightValue.text = weight;
    }else {
        self.weightValue.text = @"70";
    }
    
    // Do any additional setup after loading the view from its nib.
}

- (void)clickAvatar {
    self.imagePickController = [[UIImagePickerController alloc] init];
    self.imagePickController.delegate = self;
    self.imagePickController.allowsEditing = YES;
    
    LEEAlert.actionsheet.config
        .LeeAction(SWString(@"拍照"), ^{
            [self toCamera];
        })
        .LeeAction(SWString(@"从相册选择"), ^{
            [self toAlbum];
        })
        .LeeCancelAction(SWString(@"Cancel"), ^{
            
        })
        .LeeShow();
}
- (void)clickNameLab {
    __block UITextField *tf = nil;
    [LEEAlert alert].config
    .LeeTitle(SWString(@"Nick name"))
    .LeeAddTextField(^(UITextField *textField) {
        // 这里可以进行自定义的设置
        textField.placeholder = SWString(@"Enter nick name");
        if (@available(iOS 13.0, *)) {
            textField.textColor = [UIColor secondaryLabelColor];
        } else {
            textField.textColor = [UIColor darkGrayColor];
        }
        tf = textField; //赋值
    })
    .LeeAction(SWString(@"Confirm"), ^{
        self.nameLab.text = tf.text;
        [[NSUserDefaults standardUserDefaults] setValue:tf.text forKey:_userName];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"k_change_userInfo" object:nil];
        
    })
    .leeShouldActionClickClose(^(NSInteger index){
        // 是否可以关闭回调, 当即将关闭时会被调用 根据返回值决定是否执行关闭处理
        // 这里演示了与输入框非空校验结合的例子
        BOOL result = ![tf.text isEqualToString:@""];
        result = index == 0 ? result : YES;
        return result;
    })
    .LeeCancelAction(SWString(@"Cancel"), nil) // 点击事件的Block如果不需要可以传nil
    .LeeShow();
}

- (void)clickSexView {
    LEEAlert.actionsheet.config
    .LeeTitle(SWString(@"Gender"))
    .LeeAction(SWString(@"Male"), ^{
        [[NSUserDefaults standardUserDefaults] setValue:@"Male" forKey:_userSex];
        self.sexValue.text = SWString(@"Male");
        NSData *data = [[NSUserDefaults standardUserDefaults] valueForKey:@"userAvatar"];
        if (!data) {
            self.avatar.image = S_imageNamed(@"avatar_man_white");
        }
        [[NSNotificationCenter defaultCenter] postNotificationName:@"k_change_userInfo" object:nil];
    })
    .LeeAction(SWString(@"Female"), ^{
        [[NSUserDefaults standardUserDefaults] setValue:@"Female" forKey:_userSex];
        self.sexValue.text = SWString(@"Female");
        NSData *data = [[NSUserDefaults standardUserDefaults] valueForKey:@"userAvatar"];
        if (!data) {
            self.avatar.image = S_imageNamed(@"avatar_female_white");
        }
        [[NSNotificationCenter defaultCenter] postNotificationName:@"k_change_userInfo" object:nil];
    })
    .LeeCancelAction(SWString(@"Cancel"), ^{
        
    })
    .LeeShow();
    
}
- (void)clickAgeView {
    __block UITextField *tf = nil;
    [LEEAlert alert].config
    .LeeTitle(SWString(@"Age"))
    .LeeAddTextField(^(UITextField *textField) {
        // 这里可以进行自定义的设置
        textField.placeholder = SWString(@"Enter Age");
        if (@available(iOS 13.0, *)) {
            textField.textColor = [UIColor secondaryLabelColor];
        } else {
            textField.textColor = [UIColor darkGrayColor];
        }
        textField.keyboardType = UIKeyboardTypeNumberPad;
        tf = textField; //赋值
    })
    .LeeAction(SWString(@"Confirm"), ^{
        self.ageValue.text = [tf.text stringByAppendingString:SWString(@"year")];
        [[NSUserDefaults standardUserDefaults] setValue:tf.text forKey:_userAge];
    })
    .leeShouldActionClickClose(^(NSInteger index){
        // 是否可以关闭回调, 当即将关闭时会被调用 根据返回值决定是否执行关闭处理
        // 这里演示了与输入框非空校验结合的例子
        BOOL result = ![tf.text isEqualToString:@""];
        result = index == 0 ? result : YES;
        return result;
    })
    .LeeCancelAction(SWString(@"Cancel"), nil) // 点击事件的Block如果不需要可以传nil
    .LeeShow();
}
- (void)clickHeightView {
    __block UITextField *tf = nil;
    [LEEAlert alert].config
    .LeeTitle(SWString(@"Height"))
    .LeeAddTextField(^(UITextField *textField) {
        // 这里可以进行自定义的设置
        textField.placeholder = SWString(@"Enter height in (cm)");
        if (@available(iOS 13.0, *)) {
            textField.textColor = [UIColor secondaryLabelColor];
        } else {
            textField.textColor = [UIColor darkGrayColor];
        }
        textField.keyboardType = UIKeyboardTypeDecimalPad;
        tf = textField; //赋值
    })
    .LeeAction(SWString(@"Confirm"), ^{
        self.heightValue.text = [tf.text stringByAppendingString:@"cm"];
        [[NSUserDefaults standardUserDefaults] setValue:tf.text forKey:_userHeight];
    })
    .leeShouldActionClickClose(^(NSInteger index){
        // 是否可以关闭回调, 当即将关闭时会被调用 根据返回值决定是否执行关闭处理
        // 这里演示了与输入框非空校验结合的例子
        BOOL result = ![tf.text isEqualToString:@""];
        result = index == 0 ? result : YES;
        return result;
    })
    .LeeCancelAction(SWString(@"Cancel"), nil) // 点击事件的Block如果不需要可以传nil
    .LeeShow();
}
- (void)clickWeightView {
    __block UITextField *tf = nil;
    [LEEAlert alert].config
    .LeeTitle(SWString(@"Weight"))
    .LeeAddTextField(^(UITextField *textField) {
        // 这里可以进行自定义的设置
        textField.placeholder = SWString(@"Enter weight in (kg)");
        if (@available(iOS 13.0, *)) {
            textField.textColor = [UIColor secondaryLabelColor];
        } else {
            textField.textColor = [UIColor darkGrayColor];
        }
        textField.keyboardType = UIKeyboardTypeDecimalPad;
        tf = textField; //赋值
    })
    .LeeAction(SWString(@"Confirm"), ^{
        self.weightValue.text =  [tf.text stringByAppendingString:@"kg"];
        [[NSUserDefaults standardUserDefaults] setValue:tf.text forKey:_userWeight];
    })
    .leeShouldActionClickClose(^(NSInteger index){
        // 是否可以关闭回调, 当即将关闭时会被调用 根据返回值决定是否执行关闭处理
        // 这里演示了与输入框非空校验结合的例子
        BOOL result = ![tf.text isEqualToString:@""];
        result = index == 0 ? result : YES;
        return result;
    })
    .LeeCancelAction(SWString(@"Cancel"), nil) // 点击事件的Block如果不需要可以传nil
    .LeeShow();
}

- (void) toCamera {
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
       self.imagePickController.sourceType = UIImagePickerControllerSourceTypeCamera;
       self.imagePickController.modalPresentationStyle = UIModalPresentationFullScreen;
       [self presentViewController:self.imagePickController animated:YES completion:nil];
    } else {
        NSLog(@"无法打开相机");
    }
    
}

- (void) toAlbum  {
    self.imagePickController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    self.imagePickController.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:self.imagePickController animated:YES completion:nil];
}
// 取消选择，点击界面中的取消（Cancel）按钮时触发
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:nil];

}
 
// 选择完成，点击界面中的某个图片或者选择（Choose）按钮时触发
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info {
    dispatch_async(dispatch_get_main_queue(), ^{
        UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
        self.avatar.image = image;
        [picker dismissViewControllerAnimated:YES completion:nil];
        [[NSUserDefaults standardUserDefaults] setValue:UIImageJPEGRepresentation(image, 0.5) forKey:@"userAvatar"];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"k_change_userInfo" object:nil];

    });
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
